<?php
namespace app\common\controller;
use think\Controller;

/*智能闸机 公用Class*/
class BaseApi extends  Controller
{
	  public function _initialize()
    {
    	if(!request()->isPost()){
    		$this->ajax_error("","0000","错误的请求方式");	
    	}
    	if(empty(input("v"))){//判断版本号
    		$this->ajax_error("","0001","错误的版本号")	;
    	}
		if(empty(input("t"))){//判断时间
    		$this->ajax_error("","0003","链接已经过期");
    	}
		if(!in_array(input("key"),config("auth_key"))){//判断授权key
			  $this->ajax_error("","0002","授权值错误");
		}
		if(input("sign")!=$this->get_sign(input("key"),input("t"))){//判断签名
			  $this->ajax_error("","0002","签名错误");
		}
	}
		/**
		 * 对外api返回的模板
	 	* */
    protected function api_ajax_success($data="",$msg="成功"){
    	header('Content-Type:application/json; charset=utf-8');
    	exit(json_encode(array('flag'=>TRUE,"data"=>$data,"msg"=>$msg),JSON_UNESCAPED_UNICODE));die;
    } 
	  protected function ajax($data=""){
    	header('Content-Type:application/json; charset=utf-8');
    	exit(json_encode($data,JSON_UNESCAPED_UNICODE));die;
    } 
		/**
		 * 对外api返回的模板
		 * */
		protected function api_ajax_error($data="",$msg="未知错误"){
			header('Content-Type:application/json; charset=utf-8');
			exit(json_encode(array('flag'=>FALSE,"data"=>$data,"msg"=>$msg),JSON_UNESCAPED_UNICODE));die;
		}
	/**
	 * 成功返回的模板
	 * */
    protected function ajax_success($data="",$code="1000",$msg="成功",$is_open=false,$varify_type="0",$sound_frequency="1"){
    	header('Content-Type:application/json; charset=utf-8');
		  if(empty($msg)){
		  	$msg="成功";
		  }
    	exit(json_encode(array('flag'=>TRUE,"data"=>$data,"code"=>$code,"msg"=>$msg,"is_open"=>$is_open,"varify_type"=>$varify_type,"sound_frequency"=>$sound_frequency),JSON_UNESCAPED_UNICODE));die;
    } 
	/**
	 * 失败返回的模板
	 * */
	protected function ajax_error($data="",$code="0000",$msg="未知错误",$is_open=false,$varify_type="0",$sound_frequency="2"){
		header('Content-Type:application/json; charset=utf-8');
		if(empty($msg)){
		  	$msg="未知错误";
		  }
		exit(json_encode(array('flag'=>FALSE,"data"=>$data,"code"=>$code,"msg"=>$msg,"is_open"=>$is_open,"varify_type"=>$varify_type,"sound_frequency"=>$sound_frequency),JSON_UNESCAPED_UNICODE));die;
	}
	
	/**
	 * @param $page_count 总页数
	 * @param $page_data 数据
	 * */
	 protected function page($page_count=0,$page_data=array()){
	 		$page_cur=input("page_cur/d",0);// 当前页
			$page_size=input('page_size/d',10);//每页个数
	 		return array('page_cur'=>$page_cur,'page_size'=>$page_size,'page_count'=>$page_count,"page_data"=>$page_data);
	 }
	/**
	 * 获取签名
	 * @param $key
	 * @param $timestamp
	 * */
	 public function get_sign($key,$timestamp){
		return md5($key."_".$timestamp);
	}
		protected function to_hex_revers($value){
		  if(strlen($value)==10){//10位10进制
		      $dec=str_pad(dechex(doubleval($value)), 8,"0",STR_PAD_LEFT);
		      $bit1=substr($dec, 0,2);
			  	$bit2=substr($dec, 2,2);
					$bit3=substr($dec, 4,2);
					$bit4=substr($dec, 6,2);
					return  strtoupper($bit4.$bit3.$bit2.$bit1);
		  }else{
		  	$this->ajax_error($data="",$code="0000",$msg="卡号不是10位的10进制数");
		  }
	}
			
			protected function to_dec_revers($value){
		  	  $bit1=substr($value, 0,2);
			  	$bit2=substr($value, 2,2);
					$bit3=substr($value, 4,2);
					$bit4=substr($value, 6,2);
					$dec=hexdec($bit4.$bit3.$bit2.$bit1);		
				   return	str_pad($dec, 10,'0',STR_PAD_LEFT);		
	 }
}
